<?php

namespace Mssae\SeoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Mssae\SeoBundle\Entity\Projet;
use Mssae\SeoBundle\Form\ProjetType;
use WhiteOctober\PagerfantaBundle\WhiteOctoberPagerfantaBundle ;
use Pagerfanta\Pagerfanta ;
use Pagerfanta\Adapter\DoctrineORMAdapter ;

/**
 * Projet controller.
 *
 */
class ProjetController extends Controller
{

    public function indexAction($page)
    {
        //affiche la liste de tous les projets
        if($this->get('security.context')->isGranted('ROLE_ADMIN'))
        {
            //si le curent user est un admin alors on affiche tous les projets
            $qb = $this->_getRepository('Projet')->getQueryFindAll();
            }
        else
        {
            //sinon seulement les projets du curent user
            $idUser = $this->get('security.context')->getToken()->getUser()->getId();
            $qb = $this->_getRepository('Projet')->getQueryFindByUserId($idUser) ;
                        
        }

        $adapter = new DoctrineORMAdapter($qb);
        $pagerFanta = new Pagerfanta($adapter);
        $pagerFanta->setMaxPerPage(1);
        $pagerFanta->setCurrentPage($page);
        $entities = $pagerFanta->getCurrentPageResults();    
        
        return $this->render('MssaeSeoBundle:Projet:index.html.twig', array(
            'entities' => $entities,
            'pager'    => $pagerFanta,            
        ));
    }

    public function showAction($id)
    {
        //affiche le projet qui a l'id $id
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('MssaeSeoBundle:Projet')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Projet entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('MssaeSeoBundle:Projet:show.html.twig', array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),

        ));
    }

    public function newAction()
    {
        //affiche une form pour un nouveau projet
        $entity = new Projet();
        $form   = $this->createForm(new ProjetType(), $entity);

        return $this->render('MssaeSeoBundle:Projet:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView()
        ));
    }

    public function createAction()
    {
        //crée un nouveau projet
        $entity  = new Projet();
        $request = $this->getRequest();
        $form    = $this->createForm(new ProjetType(), $entity);
        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('projet_show', array('id' => $entity->getId())));
            
        }

        return $this->render('MssaeSeoBundle:Projet:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView()
        ));
    }
  
    public function editAction($id)
    {
        //afiche un form pour éditer un projet avec l'id $id
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('MssaeSeoBundle:Projet')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Projet entity.');
        }

        $editForm = $this->createForm(new ProjetType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('MssaeSeoBundle:Projet:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    public function updateAction($id)
    {
        //met à jour le projet avec l'id $id
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('MssaeSeoBundle:Projet')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Projet entity.');
        }

        $editForm   = $this->createForm(new ProjetType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        $request = $this->getRequest();

        $editForm->bindRequest($request);

        if ($editForm->isValid()) {
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('projet_edit', array('id' => $id)));
        }

        return $this->render('MssaeSeoBundle:Projet:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    public function deleteAction($id)
    {
        //délete un projet
        $form = $this->createDeleteForm($id);
        $request = $this->getRequest();

        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $entity = $em->getRepository('MssaeSeoBundle:Projet')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Projet entity.');
            }

            $em->remove($entity);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('projet', array('page' => 1)));
    }

    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }
    
    private function _getRepository($zNomEntity)
    {
        return $toProjets = $this->getDoctrine()
                                 ->getEntityManager()
                                 ->getRepository("MssaeSeoBundle:$zNomEntity");
    }
}
